package pers.elonnsu.springbootmall.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.elonnsu.springbootmall.common.ApiRestResponse;
import pers.elonnsu.springbootmall.common.Constant;
import pers.elonnsu.springbootmall.exception.MallExceptionEnum;
import pers.elonnsu.springbootmall.model.pojo.Category;
import pers.elonnsu.springbootmall.model.pojo.User;
import pers.elonnsu.springbootmall.model.request.AddCategoryReq;
import pers.elonnsu.springbootmall.model.request.UpdateCategoryReq;
import pers.elonnsu.springbootmall.model.vo.CategoryVO;
import pers.elonnsu.springbootmall.service.CategoryService;
import pers.elonnsu.springbootmall.service.UserService;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@RestController
public class CategoryController {
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("后台：添加新目录")
    @PostMapping("/admin/category/add")
    public ApiRestResponse<String> addCategory(@Valid AddCategoryReq addCategoryReq, HttpSession session) {
        User currentUser = (User) session.getAttribute(Constant.MALL_USER);

        if (currentUser == null) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN);
        }

        // 校验是否为管理员，是则继续操作执行
        if (userService.isAdminRole(currentUser)) {
            categoryService.addCategory(addCategoryReq);
        } else {
            return ApiRestResponse.error(MallExceptionEnum.NEED_ADMIN_LOGIN);
        }
        return ApiRestResponse.success();
    }

    @ApiOperation("后台：更新目录")
    @PostMapping("/admin/category/update")
    public ApiRestResponse<String> updateCategory(@Valid UpdateCategoryReq updateCategoryReq, HttpSession session) {
        User currentUser = (User) session.getAttribute(Constant.MALL_USER);

        if (currentUser == null) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN);
        }

        // 校验是否为管理员，是则继续操作执行
        if (userService.isAdminRole(currentUser)) {
            Category category = new Category();
            BeanUtils.copyProperties(updateCategoryReq, category);
            categoryService.update(category);
        } else {
            return ApiRestResponse.error(MallExceptionEnum.NEED_ADMIN_LOGIN);
        }
        return ApiRestResponse.success();
    }

    @ApiOperation("后台：删除目录")
    @PostMapping("/admin/category/delete")
    public ApiRestResponse<Object> deleteCategory(@RequestParam("id") Integer id) {
        categoryService.deleteById(id);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台：展示分类列表（平铺）")
    @GetMapping("/admin/category/list")
    public ApiRestResponse<Object> listCategoryForAdmin(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        PageInfo<Category> pageInfo = categoryService.listCategoryForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @ApiOperation("前台：展示分类列表（递归）")
    @GetMapping("/category/list")
    public ApiRestResponse<Object> listCategoryForCustomer() {
        List<CategoryVO> categoryVOList = categoryService.listCategoryForCustomer(0);
        return ApiRestResponse.success(categoryVOList);
    }
}
